package org.jsbd.boss.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.common.ReturnJsonCode;
import org.jsbd.boss.domian.BaseInfo;
import org.jsbd.boss.domian.EmailInfo;
import org.jsbd.boss.service.impl.EmailConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/email")
public class EmailConfigController {
	public static final Logger LOGGER = LoggerFactory.getLogger(EmailConfigController.class);

	@Autowired
	private EmailConfigService emailConfigService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String configList(Model model) {
		return "email/configList";
	}

	// 基本配置查询展示接口
	@RequestMapping(value = "/configList", params = "json")
	@ResponseBody
	public Map<String, Object> configPageList(DataGridModel model) {
		List<EmailInfo> list= emailConfigService.getAllEmailInfo();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", list);
		return map;
	}

	// 跳出新增页面
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addConfig(Model model, HttpServletResponse response) {
		return "email/add";
	}

	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void saveConfig(EmailInfo emailInfo, HttpServletResponse response,
			HttpServletRequest request) {
		emailConfigService.insertConfig(emailInfo);
		ResponseUtils.responseSuccess(response);
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public void delete(String userName, HttpServletResponse response) {
		if (StringUtils.isBlank(userName)) {
			JSONObject resultJson = new JSONObject();
			resultJson.put(ReturnJsonCode.RETURN_MSG, "userName不能为空");
			ResponseUtils.renderJson(response, resultJson.toString());
			return;
		}
		emailConfigService.deleteConfig(userName);
		ResponseUtils.responseSuccess(response);
	}
	
}
